#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <fcntl.h>
#include <string.h>
#include <config.h>
#include <systems.h>
#include <types.h>
#include <gapi.h>
#include <gobjs.h>
#include "io.h"
#include "system.h"
//ap include
#include <init_routine.h>
#include <definition_file.h>

u32 fb;
u32 xres, yres, bpp, fbsize;
char *screen_base;

unsigned char image_buffer[280000];

int graph_sys_init()
{
    fb = FBDEV;
    printf("Initializing VGA and VideoIn Controller ... ");
    /* Initialize GUI system parameters */
    xres = 800;
    yres = 600;
    bpp = 2;
    fbsize = xres * yres * bpp;
    screen_base = (char *)(fg_base + 280000);
    
    /* Initinalize hardware */
    //TV decoder
    I2C_init();
    TV_init();
    //VGA
    IOWR(VGA_CONTROLLER_BASE, 0, (u32)image_buffer); //video base
    IOWR(VGA_CONTROLLER_BASE, 1, fg_base); //foreground base
    IOWR(VGA_CONTROLLER_BASE, 2, (u32)screen_base + (800 * 350 * 2)); //GUI base
    //Video_in
    IOWR(VIDEOIN_1_BASE, 0, (u32)image_buffer);
    IOWR(VIDEOIN_2_BASE, 0, (u32)image_buffer + 0x190);
    //clean foreground
    IOWR(CLEANFOREGROUND_BASE, 0, fg_base);
    
    //register interrupts
    //init_start_sensor();
    //init_button_pio();
    
    /* Enable hardware module */
    module_enable();
    //ioboard_init();
    
    
    printf("OK\n");
    GraphicDeviceLockInit();
    return 0;
}

int graph_sys_exit()
{
    int ret = 0;
#ifndef NON_OS
    munmap(screen_base, fbsize);
    fb_close();
#endif
    GraphicDeviceLockDestroy();

    return ret;
}


